﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AutoMapper;

namespace AutoMapperSamples.Samples.NullSubstitution
{
    /// <summary>
    /// NullSubstitution 的摘要说明
    /// </summary>
    [TestClass]
    public class NullSubstitution
    {
        public NullSubstitution()
        {
            //
            //TODO:  在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 空值替代
        /// </summary>
        [TestMethod]
        public void NullSubstitutionTest()
        {
            var config = new MapperConfiguration(
                cfg => cfg.CreateMap<Source, Destination>()
                .ForMember(dest => dest.Value, opt => opt.NullSubstitute("Other Value")));

            var source = new Source { Value = null };
            var mapper = config.CreateMapper();
            var destination = mapper.Map<Source, Destination>(source);

            Assert.AreEqual("Other Value",destination.Value);
            source.Value = "Not null";

            destination = mapper.Map<Source, Destination>(source);
            Assert.AreEqual("Not null", destination.Value);
        }
    }

    public class Source
    {
        public string Value { get; set; }
    }

    public class Destination
    {
        public string Value { get; set; }
    }
}
